﻿////////////////////////////////////////////////////////////////////////////////
// Source Copyright (C) J.Fan
// @author      J.Fan
// @date        2013-01-19
// @revision    Revision in Version Control System
// @remark      
////////////////////////////////////////////////////////////////////////////////

using System.IO;
using JsonConverter.Converter;

namespace JsonConverter.Writer
{
    public class StreamJsonWriter: JsonWriter
    {
        TextWriter writer;
        bool isNeedDisposeWriter;

        public StreamJsonWriter(Stream stream,
            ConverterProvider converterProvider = null)
            : base(converterProvider)
        {
            writer = new StreamWriter(stream);
            isNeedDisposeWriter = true;
        }

        public StreamJsonWriter(TextWriter writer,
            ConverterProvider converterProvider = null)
            : base(converterProvider)
        {
            this.writer = writer;
            isNeedDisposeWriter = false;
        }

        protected override void OnWriteLiteral(string jsonString)
        {
            writer.Write(jsonString);
        }

        public override void Dispose()
        {
            base.Dispose();
            if (isNeedDisposeWriter && writer != null)
            {
                writer.Dispose();
            }
        }
    }
}
